home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / src-server / xlisp / xlsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  6.9 KB  |  282 lines

  1. /* -*-C-*-
  2. ********************************************************************************
  3. *
  4. * File:         xlsym.c
  5. * RCS:          $Header: xlsym.c,v 1.4 91/03/24 22:25:36 mayer Exp $
  6. * Description:  symbol handling routines
  7. * Author:       David Michael Betz
  8. * Created:      
  9. * Modified:     Fri Oct  4 04:12:57 1991 (Niels Mayer) mayer@hplnpm
  10. * Language:     C
  11. * Package:      N/A
  12. * Status:       X11r5 contrib tape release
  13. *
  14. * WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  15. * XLISP version 2.1, Copyright (c) 1989, by David Betz.
  16. *
  17. * Permission to use, copy, modify, distribute, and sell this software and its
  18. * documentation for any purpose is hereby granted without fee, provided that
  19. * the above copyright notice appear in all copies and that both that
  20. * copyright notice and this permission notice appear in supporting
  21. * documentation, and that the name of Hewlett-Packard and David Betz not be
  22. * used in advertising or publicity pertaining to distribution of the software
  23. * without specific, written prior permission.  Hewlett-Packard and David Betz
  24. * make no representations about the suitability of this software for any
  25. * purpose. It is provided "as is" without express or implied warranty.
  26. *
  27. * HEWLETT-PACKARD AND DAVID BETZ DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  28. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  29. * IN NO EVENT SHALL HEWLETT-PACKARD NOR DAVID BETZ BE LIABLE FOR ANY SPECIAL,
  30. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  31. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  32. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  33. * PERFORMANCE OF THIS SOFTWARE.
  34. *
  35. * See ./winterp/COPYRIGHT for information on contacting the authors.
  36. * Please send modifications, improvements and bugfixes to mayer@hplabs.hp.com
  37. * Post XLISP-specific questions/information to the newsgroup comp.lang.lisp.x
  38. *
  39. ********************************************************************************
  40. */
  41. static char rcs_identity[] = "@(#)$Header: xlsym.c,v 1.4 91/03/24 22:25:36 mayer Exp $";
  42.  
  43.  
  44. #include "xlisp.h"
  45.  
  46. /* external variables */
  47. extern LVAL obarray,s_unbound;
  48. extern LVAL xlenv,xlfenv,xldenv;
  49.  
  50. /* forward declarations */
  51. LOCAL FORWARD LVAL findprop();    /* NPM: changed this to LOCAL */
  52.  
  53. /* xlenter - enter a symbol into the obarray */
  54. LVAL xlenter(name)
  55.   char *name;
  56. {
  57.     LVAL sym,array;
  58.     int i;
  59.  
  60.     /* check for nil */
  61.     if (strcmp(name,"NIL") == 0)
  62.     return (NIL);
  63.  
  64.     /* check for symbol already in table */
  65.     array = getvalue(obarray);
  66.     i = hash(name,HSIZE);
  67.     for (sym = getelement(array,i); sym; sym = cdr(sym))
  68.     if (strcmp(name,getstring(getpname(car(sym)))) == 0)
  69.         return (car(sym));
  70.  
  71.     /* make a new symbol node and link it into the list */
  72.     xlsave1(sym);
  73.     sym = consd(getelement(array,i));
  74.     rplaca(sym,xlmakesym(name));
  75.     setelement(array,i,sym);
  76.     xlpop();
  77.  
  78.     /* return the new symbol */
  79.     return (car(sym));
  80. }
  81.  
  82. /* xlmakesym - make a new symbol node */
  83. LVAL xlmakesym(name)
  84.   char *name;
  85. {
  86.     LVAL sym;
  87.     sym = cvsymbol(name);
  88.     if (*name == ':')
  89.     setvalue(sym,sym);
  90.     return (sym);
  91. }
  92.  
  93. /* xlgetvalue - get the value of a symbol (with check) */
  94. LVAL xlgetvalue(sym)
  95.   LVAL sym;
  96. {
  97.     LVAL val;
  98.  
  99.     /* look for the value of the symbol */
  100.     while ((val = xlxgetvalue(sym)) == s_unbound)
  101.     xlunbound(sym);
  102.  
  103.     /* return the value */
  104.     return (val);
  105. }
  106.  
  107. /* xlxgetvalue - get the value of a symbol */
  108. LVAL xlxgetvalue(sym)
  109.   LVAL sym;
  110. {
  111.     register LVAL fp,ep;
  112.     LVAL val;
  113.  
  114.     /* check the environment list */
  115.     for (fp = xlenv; fp; fp = cdr(fp))
  116.  
  117.     /* check for an instance variable */
  118.     if ((ep = car(fp)) && objectp(car(ep))) {
  119.         if (xlobgetvalue(ep,sym,&val))
  120.         return (val);
  121.     }
  122.  
  123.     /* check an environment stack frame */
  124.     else {
  125.         for (; ep; ep = cdr(ep))
  126.         if (sym == car(car(ep)))
  127.             return (cdr(car(ep)));
  128.     }
  129.  
  130.     /* return the global value */
  131.     return (getvalue(sym));
  132. }
  133.  
  134. /* xlsetvalue - set the value of a symbol */
  135. xlsetvalue(sym,val)
  136.   LVAL sym,val;
  137. {
  138.     register LVAL fp,ep;
  139.  
  140.     /* look for the symbol in the environment list */
  141.     for (fp = xlenv; fp; fp = cdr(fp))
  142.  
  143.     /* check for an instance variable */
  144.     if ((ep = car(fp)) && objectp(car(ep))) {
  145.         if (xlobsetvalue(ep,sym,val))
  146.         return;
  147.     }
  148.  
  149.     /* check an environment stack frame */
  150.     else {
  151.         for (; ep; ep = cdr(ep))
  152.         if (sym == car(car(ep))) {
  153.             rplacd(car(ep),val);
  154.             return;
  155.         }
  156.     }
  157.  
  158.     /* store the global value */
  159.     setvalue(sym,val);
  160. }
  161.  
  162. /* xlgetfunction - get the functional value of a symbol (with check) */
  163. LVAL xlgetfunction(sym)
  164.   LVAL sym;
  165. {
  166.     LVAL val;
  167.  
  168.     /* look for the functional value of the symbol */
  169.     while ((val = xlxgetfunction(sym)) == s_unbound)
  170.     xlfunbound(sym);
  171.  
  172.     /* return the value */
  173.     return (val);
  174. }
  175.  
  176. /* xlxgetfunction - get the functional value of a symbol */
  177. LVAL xlxgetfunction(sym)
  178.   LVAL sym;
  179. {
  180.     register LVAL fp,ep;
  181.  
  182.     /* check the environment list */
  183.     for (fp = xlfenv; fp; fp = cdr(fp))
  184.     for (ep = car(fp); ep; ep = cdr(ep))
  185.         if (sym == car(car(ep)))
  186.         return (cdr(car(ep)));
  187.  
  188.     /* return the global value */
  189.     return (getfunction(sym));
  190. }
  191.  
  192. /* xlsetfunction - set the functional value of a symbol */
  193. xlsetfunction(sym,val)
  194.   LVAL sym,val;
  195. {
  196.     register LVAL fp,ep;
  197.  
  198.     /* look for the symbol in the environment list */
  199.     for (fp = xlfenv; fp; fp = cdr(fp))
  200.     for (ep = car(fp); ep; ep = cdr(ep))
  201.         if (sym == car(car(ep))) {
  202.         rplacd(car(ep),val);
  203.         return;
  204.         }
  205.  
  206.     /* store the global value */
  207.     setfunction(sym,val);
  208. }
  209.  
  210. /* xlgetprop - get the value of a property */
  211. LVAL xlgetprop(sym,prp)
  212.   LVAL sym,prp;
  213. {
  214.     LVAL p;
  215.     return ((p = findprop(sym,prp)) ? car(p) : NIL);
  216. }
  217.  
  218. /* xlputprop - put a property value onto the property list */
  219. xlputprop(sym,val,prp)
  220.   LVAL sym,val,prp;
  221. {
  222.     LVAL pair;
  223.     if (pair = findprop(sym,prp))
  224.     rplaca(pair,val);
  225.     else
  226.     setplist(sym,cons(prp,cons(val,getplist(sym))));
  227. }
  228.  
  229. /* xlremprop - remove a property from a property list */
  230. xlremprop(sym,prp)
  231.   LVAL sym,prp;
  232. {
  233.     LVAL last,p;
  234.     last = NIL;
  235.     for (p = getplist(sym); consp(p) && consp(cdr(p)); p = cdr(last)) {
  236.     if (car(p) == prp)
  237.         if (last)
  238.         rplacd(last,cdr(cdr(p)));
  239.         else
  240.         setplist(sym,cdr(cdr(p)));
  241.     last = cdr(p);
  242.     }
  243. }
  244.  
  245. /* findprop - find a property pair */
  246. LOCAL LVAL findprop(sym,prp)
  247.   LVAL sym,prp;
  248. {
  249.     LVAL p;
  250.     for (p = getplist(sym); consp(p) && consp(cdr(p)); p = cdr(cdr(p)))
  251.     if (car(p) == prp)
  252.         return (cdr(p));
  253.     return (NIL);
  254. }
  255.  
  256. /* hash - hash a symbol name string */
  257. int hash(str,len)
  258.   char *str;
  259. {
  260.     int i;
  261.     for (i = 0; *str; )
  262.     i = (i << 2) ^ *str++;
  263.     i %= len;
  264.     return (i < 0 ? -i : i);
  265. }
  266.  
  267. /* xlsinit - symbol initialization routine */
  268. xlsinit()
  269. {
  270.     LVAL array,p;
  271.  
  272.     /* initialize the obarray */
  273.     obarray = xlmakesym("*OBARRAY*");
  274.     array = newvector(HSIZE);
  275.     setvalue(obarray,array);
  276.  
  277.     /* add the symbol *OBARRAY* to the obarray */
  278.     p = consa(obarray);
  279.     setelement(array,hash("*OBARRAY*",HSIZE),p);
  280. }
  281.